<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="HeaderInfo">
        
using System;
using System.Configuration;
using System.Globalization;
        
namespace <xsl:value-of select="ProjectName"/>.Globalization.TimeZone {

<![CDATA[

/*
		BETTER JAVASCRIPT STYLE SOLUTION FOR THIS
		
		
		Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim scriptGetClientUTCOffset As String = "<script language=javascript>"
		scriptGetClientUTCOffset &= "var now = new Date();"
		scriptGetClientUTCOffset &= "var offset = now.getTimezoneOffset();"
		scriptGetClientUTCOffset &= "document.Form1.hidClientUTCOffset.value = offset;"
		scriptGetClientUTCOffset &= "document.Form1.submit()"
		scriptGetClientUTCOffset &= "</script>"

		If Not Page.IsPostBack Then
		If (Not Me.IsStartupScriptRegistered("Startup")) Then
		Me.RegisterStartupScript("Startup", scriptGetClientUTCOffset)
		End If
		Calendar.SelectedDate = Today
		End If
		Label1.Text = hidClientUTCOffset.Value
		DataList1.DataBind()
		End Sub



*/



    
	/// <summary>
    ///   A new timezone can be created by specifying something similar
    ///   in the application configuration
    ///   Value is the offset from UTC
    ///
    ///   Developed from an idea by Michael Brumm.
    ///
    ///   DS values are formatted Month (1-12), Day, Week of Month, Offset
    ///
    ///   LIMITATION: The configuration file will need to be changed for changes
    ///               in DS dates around the world.
    ///     
    ///   Good Resource:
    ///   http://www.timeanddate.com/time/aboutdst.html
    ///
	/// 	<add key="London" value="0"/>
	///	    <add key="London_DS_Start" value="3|Sunday|4|1"/>
	///	    <add key="London_DS_End" value="10|Sunday|4|1"/>
	/// </summary>
	[Serializable()]
	public class ExtendedTimeZone : System.TimeZone
	{
		private bool _standardAlways;
		//private bool _daylightAlwaysWithinStandard;
		//private bool _standardAlwaysWithinDaylight;
		private TimeSpan _standardOffset;
		private string _standardName;
		private string _standardAbbreviation;
		private TimeSpan _daylightDelta;
		private TimeSpan _daylightOffset;
		private string _daylightName;
		private string _daylightAbbreviation;
		private DaylightTimeChange _daylightTimeChangeStart;
		private DaylightTimeChange _daylightTimeChangeEnd;

		private ExtendedTimeZone()
		{
		}

		/// <summary>
		/// Load from config file
		/// </summary>
		public ExtendedTimeZone(string standardName) 
		{
			_standardOffset=
				new TimeSpan(int.Parse(ConfigurationSettings.AppSettings[standardName]),0,0);

			_standardAbbreviation=_standardName=standardName;

			if(ConfigurationSettings.AppSettings[standardName+"_DS_Start"]==null) 
			{
				_standardAlways=true;
			} 
			else 
			{

				string[] daylightStart=
					ConfigurationSettings.AppSettings[standardName+"_DS_Start"].Split('|');

				string[] daylightEnd=
					ConfigurationSettings.AppSettings[standardName+"_DS_End"].Split('|');

				_daylightDelta=new TimeSpan(1,0,0);
				_daylightOffset=_standardOffset.Add(_daylightDelta);
				
				_daylightAbbreviation=_daylightName=standardName+" (daylight)";


				_daylightTimeChangeStart=new DaylightTimeChange(int.Parse(daylightStart[0]),
					(DayOfWeek)Enum.Parse(typeof(DayOfWeek),daylightStart[1],true),
					int.Parse(daylightStart[2]),new TimeSpan(int.Parse(daylightStart[3]),0,0));


				_daylightTimeChangeEnd=new DaylightTimeChange(int.Parse(daylightEnd[0]),
					(DayOfWeek)Enum.Parse(typeof(DayOfWeek),daylightEnd[1],true),
					int.Parse(daylightEnd[2]),new TimeSpan(int.Parse(daylightEnd[3]),0,0));

				
			}
			

		}

		public ExtendedTimeZone(TimeSpan standardOffset, string standardName, 
			string standardAbbreviation)
		{
			_standardAlways = true;
			_standardOffset = standardOffset;
			_standardName = standardName;
			_standardAbbreviation = standardAbbreviation;
		}

		public ExtendedTimeZone(TimeSpan standardOffset, string standardName, 
			string standardAbbreviation, TimeSpan daylightDelta, 
			string daylightName, string daylightAbbreviation, 
			DaylightTimeChange daylightTimeChangeStart, 
			DaylightTimeChange daylightTimeChangeEnd)
		{
			if (((daylightTimeChangeStart == null) && (daylightTimeChangeEnd == null))) 
			{
				_standardAlways = true;
				_standardOffset = standardOffset;
				_standardName = standardName;
				_standardAbbreviation = standardAbbreviation;
				return;
			}
			if ((daylightTimeChangeStart == null)) 
			{
				throw new ArgumentNullException("daylightTimeChangeStart");
			}
			if ((daylightTimeChangeEnd == null)) 
			{
				throw new ArgumentNullException("daylightTimeChangeEnd");
			}
			_standardAlways = false;
			_standardOffset = standardOffset;
			_standardName = standardName;
			_standardAbbreviation = standardAbbreviation;
			_daylightDelta = daylightDelta;
			_daylightOffset = _standardOffset.Add(daylightDelta);
			_daylightName = daylightName;
			_daylightAbbreviation = daylightAbbreviation;
			_daylightTimeChangeStart = daylightTimeChangeStart;
			_daylightTimeChangeEnd = daylightTimeChangeEnd;
		}

		public override string StandardName 
		{
			get 
			{
				return _standardName;
			}
		}

		public virtual string StandardAbbreviation 
		{
			get 
			{
				return _standardAbbreviation;
			}
		}

		public override string DaylightName 
		{
			get 
			{
				return _daylightName;
			}
		}

		public virtual string DaylightAbbreviation 
		{
			get 
			{
				return _daylightAbbreviation;
			}
		}

		public virtual string GetNameLocalTime(DateTime time)
		{
			if ((_standardAlways)) 
			{
				return _standardName;
			} 
			else if ((IsDaylightSavingTime(time))) 
			{
				return _daylightName;
			} 
			else 
			{
				return _standardName;
			}
		}

		public virtual string GetNameUniversalTime(DateTime time)
		{
			if ((IsDaylightSavingTimeUniversalTime(time))) 
			{
				return _daylightName;
			} 
			else 
			{
				return _standardName;
			}
		}

		public virtual string GetAbbreviationLocalTime(DateTime time)
		{
			if ((_standardAlways)) 
			{
				return _standardAbbreviation;
			} 
			else if ((IsDaylightSavingTime(time))) 
			{
				return _daylightAbbreviation;
			} 
			else 
			{
				return _standardAbbreviation;
			}
		}

		public virtual string GetAbbreviationUniversalTime(DateTime time)
		{
			if ((IsDaylightSavingTimeUniversalTime(time))) 
			{
				return _daylightAbbreviation;
			} 
			else 
			{
				return _standardAbbreviation;
			}
		}

		public override DaylightTime GetDaylightChanges(Int32 year)
		{
			if (((year < 1) || (year > DateTime.MaxValue.Year))) 
			{
				throw new ArgumentOutOfRangeException("year");
			}
			if ((_standardAlways)) 
			{
				return null;
			} 
			else 
			{
				return new DaylightTime(_daylightTimeChangeStart.GetDate(year), 
					_daylightTimeChangeEnd.GetDate(year), _daylightDelta);
			}
		}

		public override bool IsDaylightSavingTime(DateTime time)
		{
			return IsDaylightSavingTime(time, false);
		}

		public virtual bool IsDaylightSavingTimeUniversalTime(DateTime time)
		{
			time = time.Add(_standardOffset);
			return IsDaylightSavingTime(time, true);
		}

		private bool IsDaylightSavingTime(DateTime time, bool fromUtcTime)
		{
			if ((_standardAlways)) 
			{
				return false;
			}
			DaylightTime daylightTimes;
			daylightTimes = GetDaylightChanges(time.Year);
			return IsDaylightSavingTime(time, daylightTimes, fromUtcTime);
		}

		public static new bool IsDaylightSavingTime(DateTime time, 
											DaylightTime daylightTimes)
		{
			return IsDaylightSavingTime(time, daylightTimes, false);
		}

		private static bool IsDaylightSavingTime(DateTime time, 
			DaylightTime daylightTimes, bool fromUtcTime)
		{
			if ((daylightTimes == null)) 
			{
				return false;
			}
			DateTime daylightStart;
			DateTime daylightEnd;
			TimeSpan daylightDelta;
			daylightStart = daylightTimes.Start;
			daylightEnd = daylightTimes.End;
			daylightDelta = daylightTimes.Delta;
			if ((fromUtcTime)) 
			{
				daylightEnd = daylightEnd.Subtract(daylightDelta);
			}
			if ((daylightStart < daylightEnd)) 
			{
				if (((time >= daylightStart) && (time < daylightEnd))) 
				{
					if ((fromUtcTime)) 
					{
						return true;
					} 
					else 
					{
						if ((daylightDelta.Ticks > 0)) 
						{
							if ((time < (daylightStart.Add(daylightDelta)))) 
							{
								return false;
							} 
							else 
							{
								return true;
							}
						} 
						else 
						{
							return true;
						}
					}
				} 
				else 
				{
					if ((fromUtcTime)) 
					{
						return false;
					} 
					else 
					{
						if ((daylightDelta.Ticks < 0)) 
						{
							if (((time >= daylightEnd) && (time < 
								daylightEnd.Subtract(daylightDelta)))) 
							{
								return true;
							} 
							else 
							{
								return false;
							}
						} 
						else 
						{
							return false;
						}
					}
				}
			} 
			else 
			{
				if ((time >= daylightStart)) 
				{
					if ((fromUtcTime)) 
					{
						return true;
					} 
					else 
					{
						if ((daylightDelta.Ticks > 0)) 
						{
							if ((time < (daylightStart.Add(daylightDelta)))) 
							{
								return false;
							} 
							else 
							{
								return true;
							}
						} 
						else 
						{
							return true;
						}
					}
				} 
				else if ((time < daylightEnd)) 
				{
					return true;
				} 
				else 
				{
					if ((fromUtcTime)) 
					{
						return false;
					} 
					else 
					{
						if ((daylightDelta.Ticks < 0)) 
						{
							if (((time >= daylightEnd) && (time < 
								daylightEnd.Subtract(daylightDelta)))) 
							{
								return true;
							} 
							else 
							{
								return false;
							}
						} 
						else 
						{
							return false;
						}
					}
				}
			}
		}

		public virtual bool IsAmbiguous(DateTime time)
		{
			if ((_standardAlways)) 
			{
				return false;
			}
			DaylightTime daylightTimes;
			daylightTimes = GetDaylightChanges(time.Year);
			return IsAmbiguous(time, daylightTimes);
		}

		public static bool IsAmbiguous(DateTime time, DaylightTime daylightTimes)
		{
			if ((daylightTimes == null)) 
			{
				return false;
			}
			DateTime daylightStart;
			DateTime daylightEnd;
			TimeSpan daylightDelta;
			daylightStart = daylightTimes.Start;
			daylightEnd = daylightTimes.End;
			daylightDelta = daylightTimes.Delta;
			if ((daylightDelta.Ticks > 0)) 
			{
				if (((time < daylightEnd) && (daylightEnd.Subtract(daylightDelta) <= time))) 
				{
					return true;
				}
			} 
			else if ((daylightDelta.Ticks < 0)) 
			{
				if (((time < daylightStart) && (daylightStart.Add(daylightDelta) <= time))) 
				{
					return true;
				}
			}
			return false;
		}

		public override TimeSpan GetUtcOffset(DateTime time)
		{
			if ((_standardAlways)) 
			{
				return _standardOffset;
			} 
			else if ((IsDaylightSavingTime(time))) 
			{
				return _daylightOffset;
			} 
			else 
			{
				return _standardOffset;
			}
		}

		public override DateTime ToLocalTime(DateTime time)
		{
            
            if(_standardOffset.Ticks>=0) if(DateTime.MaxValue.Subtract(_standardOffset)<time) return time;
			
            time = time.Add(_standardOffset);
            
		    if ((!((_standardAlways)))) 
		    {
			    if ((IsDaylightSavingTime(time, true))) 
			    {
				    time = time.Add(_daylightDelta);
			    }
		    }

			return time;
		}

		public override DateTime ToUniversalTime(DateTime time)
		{
            try {
			    if ((_standardAlways)) 
			    {
				    return time.Subtract(_standardOffset);
			    } 
			    else 
			    {
				    if ((IsDaylightSavingTime(time))) 
				    {
                        if(DateTime.MinValue.Add(_daylightOffset)>time) return time;
					    return time.Subtract(_daylightOffset);
				    } 
				    else 
				    {
                        if(DateTime.MinValue.Add(_standardOffset)>time) return time;
					    return time.Subtract(_standardOffset);
				    }
			    }
            } catch ( Exception ex ) {
                // TODO: Handle
            }
            return time;
		}

		public DateTime ToUniversalTime(DateTime time, bool daylightSaving)
		{
            try {
			    if ((_standardAlways)) 
			    {
                    if(DateTime.MinValue.Add(_standardOffset)>time) return time;
				    return time.Subtract(_standardOffset);
			    } 
			    else 
			    {
				    if ((daylightSaving)) 
				    {
                        if(DateTime.MinValue.Add(_daylightOffset)>time) return time;
					    return time.Subtract(_daylightOffset);
				    } 
				    else 
				    {
                        if(DateTime.MinValue.Add(_standardOffset)>time) return time;
					    return time.Subtract(_standardOffset);
				    }
			    }
            } catch ( Exception ex ) {
                // TODO: Handle
            }
            return time;
		}
	}
    
    
    
	[Serializable()]
	public class DaylightTimeChange
	{
		private const Int32 NUM_DAYS_IN_WEEK = 7;
		private Int32 _month;
		private DayOfWeek _dayOfWeek;
		private Int32 _dayOfWeekIndex;
		private TimeSpan _timeOfDay;

		private DaylightTimeChange()
		{
		}

		public DaylightTimeChange(Int32 month, DayOfWeek dayOfWeek, 
			Int32 dayOfWeekIndex, TimeSpan timeOfDay)
		{
			if (((month < 1) || (month > 12))) 
			{
				throw new ArgumentOutOfRangeException("month", month, 
						"The month must be between 1 and 12, inclusive.");
			}
			if (((dayOfWeek < DayOfWeek.Sunday) || (dayOfWeek > DayOfWeek.Saturday))) 
			{
				throw new ArgumentOutOfRangeException("dayOfWeek", dayOfWeek, 
						"The day of week must be between Sunday and Saturday.");
			}
			if (((dayOfWeekIndex < 0) || (dayOfWeekIndex > 4))) 
			{
				throw new ArgumentOutOfRangeException("dayOfWeekIndex", 
					dayOfWeekIndex, 
					"The day of week index must be between 0 and 4, inclusive.");
			}

			if (((timeOfDay.Ticks < 0) || (timeOfDay.Ticks >= TimeSpan.TicksPerDay))) 
			{
				throw new ArgumentOutOfRangeException("timeOfDay", timeOfDay, 
					"The time of the day must be less than one day, " + 
					"and not negative.");
			}

			_month = month;
			_dayOfWeek = dayOfWeek;
			_dayOfWeekIndex = dayOfWeekIndex;
			_timeOfDay = timeOfDay;
		}

		public virtual DateTime GetDate(Int32 year)
		{
			if (((year < 1) || (year > DateTime.MaxValue.Year))) 
			{
				throw new ArgumentOutOfRangeException("year");
			}

			DateTime resultDate = new DateTime(year, _month, 1);
			if ((resultDate.DayOfWeek > _dayOfWeek)) 
			{
				resultDate = resultDate.AddDays(NUM_DAYS_IN_WEEK - 
									(resultDate.DayOfWeek - _dayOfWeek));
			} 

			else if ((resultDate.DayOfWeek < _dayOfWeek)) 
			{
				resultDate = resultDate.AddDays(_dayOfWeek - resultDate.DayOfWeek);
			}

			resultDate = resultDate.AddDays(NUM_DAYS_IN_WEEK * _dayOfWeekIndex);

			while ((resultDate.Month > _month)) 
			{
				resultDate = resultDate.AddDays(-NUM_DAYS_IN_WEEK);
			}
			resultDate = resultDate.Add(_timeOfDay);
			return resultDate;
		}
	}

]]>
    
}
        
        
    </xsl:template>
</xsl:stylesheet>